package com.hotel.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.MasterCreator;
import com.hotel.BL.TaxLogic;

import data.Tax;
import data.passingData.ResultData;

@SuppressWarnings("serial")

public class TaxService extends HttpServlet{	
	//http://localhost:8080/taxService?func=addTax&title=1&shortname=1&CurrencyValue=1&currencyString=1&IsPercent=true&IsOnRoomTariff=true&IsPerReservation=true&description=aa
	//http://localhost:8080/taxService?func=editTax&&taxId=1&title=222222222222222&shortname=1&CurrencyValue=1&currencyString=1&IsPercent=true&IsOnRoomTariff=true&IsPerReservation=true&description=aa
	//http://localhost:8080/taxService?func=delTax&taxId=828
	//http://localhost:8080/taxService?func=changeTax&taxId=828
	//http://localhost:8080/taxService?func=getRoomTax
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addTax")){
				AddTax(req,resp);
			}else if(doClass.equals("editTax")){
				EditTax(req,resp);
			}else if(doClass.equals("delTax")){
				DeleteTax(req,resp);
			}else if(doClass.equals("changeTax")){
				ChangeStatus(req, resp);
			}else if(doClass.equals("calTax")){
				CalTax(req, resp);
			}else if(doClass.equals("getShowTax")){
				GetShowTax(req,resp);
			}else if(doClass.equals("editSortOrder")){
				EditSortOrder(req,resp);
			}else if(doClass.equals("getAllTax")){
				GetAllTax(req,resp);
			}else if(doClass.equals("getRoomTax")){
				GetRoomTax(req,resp);
			}else if(doClass.equals("getTaxById")){
				GetTaxById(req,resp);
			}else if(doClass.equals("CalChargeTax")){
				CalChargeTax(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String shortname = req.getParameter("shortname");
		Double CurrencyValue = Double.parseDouble(req.getParameter("CurrencyValue"));
		String currencyString = String.valueOf(new MasterCreator().GetCurrency("THB").getId());
		boolean IsOnRoomTariff = Boolean.parseBoolean(req.getParameter("IsOnRoomTariff"));
		String description = req.getParameter("description");
		
		new TaxLogic(req,resp).AddTax(hotelId,shortname,CurrencyValue,currencyString,IsOnRoomTariff,description);
		
		GetAllTax(req, resp);
	}

	private void EditTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long taxId = Long.parseLong(req.getParameter("taxId"));
		String shortname = req.getParameter("shortname");
		Double CurrencyValue = Double.parseDouble(req.getParameter("CurrencyValue"));
		String currencyString = req.getParameter("currencyString");
		boolean IsOnRoomTariff = Boolean.parseBoolean(req.getParameter("isOnRoomTariff"));
		String description = req.getParameter("description");				
		
		new TaxLogic(req,resp).EditTax(taxId,shortname,CurrencyValue,currencyString,IsOnRoomTariff,description);
		
		GetAllTax(req, resp);
	}

	private void DeleteTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String taxIdList = req.getParameter("taxId");
		for(int i = 0; i < taxIdList.split(",").length ; i++){
			new TaxLogic(req,resp).DeleteTax(Long.parseLong(taxIdList.split(",")[i]));
		}
		
		GetAllTax(req, resp);
	}

	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long taxId = Long.parseLong(req.getParameter("taxId"));

		Object obj = new TaxLogic(req,resp).ChangeStatus(taxId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	private void CalTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		long hotelId = (Long) session.getAttribute("hotelId");
		Double amount = Double.parseDouble(req.getParameter("amount"));
		TaxLogic taxLogic = new TaxLogic(req,resp);
		Object obj = taxLogic.CalTax(hotelId, amount);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditSortOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long id = Long.parseLong(req.getParameter("id"));
		long sortOrder = Long.parseLong(req.getParameter("sortOrder"));

		Tax a = new TaxLogic(req,resp).EditSortOrder(id, sortOrder);
		String x = "T";
		if(a == null){
			x = "F";
		}

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(x));
	}
	
	private void GetAllTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		Object obj = new TaxLogic(req,resp).GetAllTax4AdminLimit(hotelId,offset,line);
		double dataSize = new TaxLogic(req,resp).GetAllTax4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetRoomTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		Object obj = new TaxLogic(req,resp).GetRoomTax(hotelId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetTaxById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Long id = Long.parseLong(req.getParameter("id"));
		Tax obj = new TaxLogic(req,resp).GetTax(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetShowTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<Tax> obj = new TaxLogic(req,resp).GetAllTax(hotelId);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void CalChargeTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		String charge = req.getParameter("charge");
		boolean incTaxStatus = Boolean.parseBoolean(req.getParameter("incTax"));
		List<Object> list =  new ArrayList<Object>();
		String[] chAry = charge.split(",");
		for(int i = 0;i<chAry.length;i++){
		HashMap<String,Double> calTax =new TaxLogic(req,resp).CalRoomTaxWithPolicy(hotelId,Double.parseDouble(chAry[i]),incTaxStatus);
		list.add(calTax);
		}
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(list));
	}
}
